package com.xtourms.client.sys.um;

import java.util.Stack;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.ContentFrame;
import com.xtourms.client.Service;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.client.util.Util;
import com.xtourms.shared.field.WorkerField;

public class WorkerManagerFrame extends Composite implements ContentFrame{

	private static UserManagerFrameUiBinder uiBinder = GWT
			.create(UserManagerFrameUiBinder.class);

	interface UserManagerFrameUiBinder extends
			UiBinder<Widget, WorkerManagerFrame> {
	}

	protected Stack<Widget> widgetManager = new Stack<Widget>();
	private ContentFrame cf;
	@UiField HTMLPanel wmf;
	@UiField TextBox worker_name;
	@UiField ListBox worker_dep;
	
	public WorkerManagerFrame() {
		initWidget(uiBinder.createAndBindUi(this));
		this.cf = this;
		initSearchPanel();
	}

	private void initSearchPanel() {
		Util.fillListBox(worker_dep, WorkerField.DEP_STRS);
		worker_dep.insertItem("不限", 0);
	}

	@UiHandler("search")
	void search(ClickEvent evt){
		JSONObject condObj = new JSONObject();
		condObj.put(WorkerField.WORKER_NAME, new JSONString(worker_name.getValue()));
		if(0 != worker_dep.getSelectedIndex()){
			condObj.put(WorkerField.WORKER_DEP, new JSONNumber(worker_dep.getSelectedIndex()));
		}
		Service.umService.queryWorker(condObj.toString(), new AsyncCallback<String>() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(String result) {
				JSONArray rets = (JSONArray) JSONParser.parse(result);
				resetContent();
				appendContent(new WorkerManager(rets, cf));
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.ins.showError("查询员工服务错误："+caught.getMessage());
			}
		});
	}
	
	@UiHandler("reset")
	void reset(ClickEvent evt){
		worker_name.setValue("");
		worker_dep.setSelectedIndex(0);
	}

	public void appendContent(Widget next){
		if(0 != widgetManager.size()){	
			Widget per = widgetManager.peek();
			per.removeFromParent();
		}
		widgetManager.push(next);
		wmf.add(next, "um_content");
	}
	
	public void resumeContent(){
		if(1 < widgetManager.size()){
			Widget cur = widgetManager.pop();
			cur.removeFromParent();
			cur = widgetManager.peek();
			wmf.add(cur, "um_content");
		}
	}
	
	public void resetContent() {
		if(!widgetManager.isEmpty()){
			Widget cur = widgetManager.pop();
			cur.removeFromParent();
		}
		widgetManager.clear();
	}
}
